package com.chushouya.controller.manager.admin;


import com.chushouya.common.annotations.AuthSkip;
import com.chushouya.common.constant.ServiceType;
import com.chushouya.common.support.Contexts;
import com.chushouya.manager.dto.admin.login.AccountLoginDTO;
import com.chushouya.manager.dto.admin.login.LoginUser;
import com.chushouya.manager.dto.admin.menu.RouterVO;
import com.chushouya.manager.service.admin.AdminLoginService;
import com.chushouya.manager.service.admin.AdminUserService;
import com.chushouya.manager.service.admin.MenuService;
import com.general.framework.core.constant.Constants;
import com.general.framework.core.enums.BusinessType;
import com.general.framework.core.enums.ContextUserType;
import com.general.framework.core.enums.LimitType;
import com.general.framework.core.schema.Response;
import com.general.framework.web.annotations.Log;
import com.general.framework.web.annotations.RateLimiter;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = ServiceType.PLATFORM + "登录中心")
@RestController
@RequestMapping(ServiceType.PLATFORM + "/adminUserCenter")
public class AdminUserCenterController {

    @Resource
    private AdminLoginService adminLoginService;

    @Resource
    private MenuService menuService;

    @Resource
    private AdminUserService adminUserService;

    @ApiOperation("管理员账号登录")
    @PostMapping("/adminLogin")
    @RateLimiter(time = 5, count = 3, limitType = LimitType.IP)
    @AuthSkip
    public Response<Map> adminLogin(@Validated @RequestBody AccountLoginDTO accountLogin) {
        // 生成令牌
        String token = adminLoginService.accountLogin(ContextUserType.ADMIN_PLATFORM, accountLogin);
        Map<String, String> loginResult = new HashMap<>();
        loginResult.put(Constants.TOKEN, token);
        return Response.success(loginResult);
    }

    @ApiOperation("获取登录用户信息")
    @GetMapping("/getLoginUser")
    public Response<LoginUser> getLoginUser() {
        return Response.success(adminLoginService.getLoginUser(Contexts.getHeaderContext().getToken()));
    }

    @ApiOperation("获取路由信息")
    @GetMapping("/getRouters")
    public Response<List<RouterVO>> getRouters() {
        return Response.success(menuService.getRouter(Contexts.getUserContext()));
    }

    @ApiOperation("退出登录")
    @PostMapping("/logout")
    public Response<Void> logout() {
        adminLoginService.logout(Contexts.getHeaderContext().getToken());
        return Response.success();
    }

    @ApiOperation("修改密码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "oldPassword", value = "原始密码", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "password", value = "新密码", dataType = "String", paramType = "query")
    })
    @Log(businessType = BusinessType.UPDATE)
    @PostMapping("/updatePassword")
    public Response<Void> updatePassword(String oldPassword, String password) {
        adminUserService.updatePassword(Contexts.getUserContext().getUserId(), oldPassword, password);
        return Response.success();
    }

}
